package com.examples.springboot.jpa.pojos;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.examples.springboot.jpa.domain.ClimberPhoneNumber;
import com.examples.springboot.jpa.domain.Mount;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Builder
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ClimberDto {
  private String id;
  private String name;
  private Set<Mount> climbedMounts;
  private Set<ClimberPhoneNumber> phoneNumbers;

  public Mount getMount() {
    return climbedMounts==null||climbedMounts.isEmpty()?null:climbedMounts.iterator().next();
  }
  
  public void setMount(Mount mount) {
    if(climbedMounts==null)
      climbedMounts=new HashSet<Mount>();
    climbedMounts.add(mount);
  }

  public ClimberPhoneNumber getPhoneNumber() {
    return phoneNumbers==null||phoneNumbers.isEmpty()?null:phoneNumbers.iterator().next();
  }
  
  public void setPhoneNumber(ClimberPhoneNumber phoneNumber) {
    if(phoneNumbers==null)
      phoneNumbers=new HashSet<ClimberPhoneNumber>();
    phoneNumbers.add(phoneNumber);
  }

  @Override
  public String toString() {
    Iterator<?> it;
    boolean hasNextElement = false;
    StringBuilder sb = new StringBuilder("{\"id\":\"").append(id);
    sb.append("\", \"name\":\"").append(name!=null?name:"").append('"');

    if(climbedMounts!=null) {
      sb.append(",\"climbedMounts\":[");
      it = climbedMounts.iterator();
      while (it.hasNext()) {
        if(hasNextElement)
          sb.append(',');
        sb.append(it.next().toString());
        hasNextElement=true;
      }
      sb.append(']');
    }

    hasNextElement = false;
    if (phoneNumbers != null) {
      sb.append(",\"phoneNumbers\":[");
      it = phoneNumbers.iterator();
      while (it.hasNext()) {
        if(hasNextElement)
          sb.append(',');
        sb.append(it.next().toString());
        hasNextElement=true;
      }
      sb.append(']');
    }

    return sb.append('}').toString();
  }
  
}
